/**
 * @Title: ExampleFile.java
 * @Package com.example.package
 * @Description: Placeholder (briefly describe the purpose of this file)
 * @Author: Evan
 * @Date: 2023-08-18
 * @Version: V1.0
 *
 * Copyright Notice:
 * This project is released in open source and follows the applicable open source license.
 * All rights reserved. (c) 2023 Evan.
 * This project is protected by copyright laws and international copyright treaties.
 * Unauthorized reproduction, sale, copy, modification, merge, publication, distribution,
 * licensing, and/or sales of this software and its copies are prohibited without permission.
 * Please contact the author for authorization.
 * Contact Information:
 * @Email: uyevan@163.com
 */

package com.evan.biznak.vip.Util;

import static com.evan.biznak.vip.Config.Config.APP_KEY;

import android.content.Context;

import org.apache.commons.codec.binary.Base64;

import java.util.Objects;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    public static String encrypt(String input, Context context) {
        byte[] crypted = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(FileUtil.evanVip(APP_KEY, context).getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return new String(Objects.requireNonNull(Base64.encodeBase64(crypted)));
    }

    public static String decrypt(String input, Context context) {
        byte[] output = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(FileUtil.evanVip(APP_KEY, context).getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(Base64.decodeBase64(input));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        assert output != null;
        return new String(output);
    }
}